package geppetto.main;

import geppetto.phraseTable.phrase.Phrase;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class PurposeBasedDivision {
	protected static String reference;
	protected static String source;
	protected static String output;
	protected static String encoding;
	protected static String conf;

	public static void main(String[] args) throws Exception{

		source = args[0];
		reference = args[1];
		output = args[2];
		int numberOfRefs = Integer.parseInt(args[3]);
		conf = args[4];
		encoding = args[5];

		BufferedReader sourceReader = new BufferedReader(new InputStreamReader(new FileInputStream(source), encoding));
		BufferedReader referenceReader = new BufferedReader(new InputStreamReader(new FileInputStream(reference), encoding));
		PrintStream affirmationWriter1 = new PrintStream(new File(output + ".affirmations.s"), encoding);
		PrintStream affirmationWriter2 = new PrintStream(new File(output + ".affirmations.t"), encoding);
		PrintStream interrogationWriter1 = new PrintStream(new File(output + ".interrogation.s"), encoding);
		PrintStream interrogationWriter2 = new PrintStream(new File(output + ".interrogation.t"), encoding);
		PrintStream exclamationWriter1 = new PrintStream(new File(output + ".exclamation.s"), encoding);
		PrintStream exclamationWriter2 = new PrintStream(new File(output + ".exclamation.t"), encoding);
		PrintStream declarationWriter1 = new PrintStream(new File(output + ".declaration.s"), encoding);
		PrintStream declarationWriter2 = new PrintStream(new File(output + ".declaration.t"), encoding);
		while(sourceReader.ready() || referenceReader.ready()){
			if(!(referenceReader.ready() && sourceReader.ready())){
				System.out.println("the files are not the same size");
				System.exit(-1);
			}
			String line1 = sourceReader.readLine();
			String line2 = referenceReader.readLine();			
			String referenceText = line2 + "\n";
			for(int i = 1; i < numberOfRefs; i++){
				referenceText += referenceReader.readLine() + "\n";
			}

			if(detectAffirmation(line1, line2)){
				affirmationWriter1.println(line1);
				affirmationWriter2.print(referenceText);
			}
			if(detectDeclaration(line1, line2)){
				declarationWriter1.println(line1);
				declarationWriter2.print(referenceText);
			}
			if(detectInterrogation(line1, line2)){
				interrogationWriter1.println(line1);
				interrogationWriter2.print(referenceText);				
			}
			if(detectExclamation(line1, line2)){
				exclamationWriter1.println(line1);
				exclamationWriter2.print(referenceText);			
			}
		}
	}

	public static boolean detectAffirmation(String sourceLine, String targetLine){
		if(conf.equals("contains")){
			if(sourceLine.contains(".")){
				return true;
			}
		}
		if(conf.equals("lastChar")){
			if(sourceLine.endsWith(".")){
				return true;
			}
		}
		return false;
	}

	public static boolean detectDeclaration(String sourceLine, String targetLine){
		if(conf.equals("contains")){
			if(sourceLine.contains(".")){
				return true;
			}
		}
		if(conf.equals("lastChar")){
			if(sourceLine.endsWith(".")){
				return true;
			}
		}
		return false;
	}

	public static boolean detectInterrogation(String sourceLine, String targetLine){
		if(conf.equals("contains")){
			if(sourceLine.contains("?")){
				return true;
			}
		}
		if(conf.equals("lastChar")){
			if(sourceLine.endsWith("?")){
				return true;
			}
		}
		return false;
	}

	public static boolean detectExclamation(String sourceLine, String targetLine){
		if(conf.equals("contains")){
			if(sourceLine.contains("!")){
				return true;
			}
		}
		if(conf.equals("lastChar")){
			if(sourceLine.endsWith("!")){
				return true;
			}
		}
		return false;
	}
}
